/**
 * 刷新页面
 * @returns {boolean}
 */
function rePage() {
    window.location.href = window.location.href.replace('#', '');
    return false;
}
/**
 * 返回上一页
 */
function back() {
    window.history.back(-1);
}
/**
 * 弹窗
 * @param title 标题
 * @param content 地址
 * @param width 宽
 * @param height 高
 */
function openDialog(title, content, width, height) {
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    var index = layer.open({
        title: title,
        type: 2,
        area: [width, height],
        content: content
    });
    return index;
}
/**
 * 跳转页面
 * @param url
 * @returns {boolean}
 * @constructor
 */
function Urlhref(url) {
    window.location.href = url;
    return false;
}
/**
 * 全选
 * @constructor
 */
function CheckAll() {
    $("#grid :checkbox").prop("checked", true);
}
/**
 * 反选
 * @constructor
 */
function CheckOther() {
    $("#grid :checkbox").each(function () {
        $(this).prop("checked", !$(this).prop("checked"));
    });
}
/**
 获取选中复选框值
 值：1,2,3,4
 **/
function CheckboxValue() {
    var reVal = '';
    $("#grid :checkbox").each(function () {
        if ($(this).prop("checked")) {
            reVal += $(this).val() + ",";
        }
    });
    reVal = reVal.substr(0, reVal.length - 1);
    return reVal;
}
/**
 * 单条复选框数据
 * @param reVal
 * @returns {boolean}
 * @constructor
 */
function IsOne(reVal) {
    var isOK = true;
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    if (reVal == undefined || reVal == "") {
        isOK = false;
        layer.msg('请选择要操作的数据', {icon: 2});
    } else if (reVal.split(",").length > 1) {
        isOK = false;
        layer.msg('对不起，只能选择一条数据', {icon: 2});
    }
    return isOK;
}
/**
 * 多条复选框数据
 * @param reVal
 * @returns {boolean}
 * @constructor
 */
function IsMore(reVal) {
    var isOK = true;
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    if (reVal == undefined || reVal == "") {
        isOK = false;
        layer.msg('请选择要操作的数据!', {icon: 2});
    }
    return isOK;
}
/**
 * Ajax请求
 * @param url 地址
 * @param parm 参数
 * @param callBack 回调方法
 */
function getAjax(url, parm, callBack) {
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    $.ajax({
        type: 'post',
        url: url,
        data: parm,
        cache: false,
        async: false,
        beforeSend: function (data) {
            layer.load(2);
        },
        success: function (data) {
            layer.closeAll();
            callBack(data);
        },
        error: function () {
            layer.closeAll();
            layer.alert('与服务器断开连接！', {icon: 2});
            return false;
        }
    });
}
/**
 * 确认操作
 * @param msg
 * @param url
 * @param params
 */
function doConfirm(msg, url, params,callBack) {
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    layer.confirm(msg, {icon: 3, title: '提示消息'}, function (index) {
        layer.close(index);
        getAjax(url, params, function (data) {
            callBack(data);
        });
    });
}
/**
 * 确认操作数据
 * @param obj 对象
 * @param msg 提醒消息
 */
function doAction(obj) {
    var msg = $(obj).data('msg');
    var url = $(obj).data('url');
    var params = $(obj).data('params');
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    doConfirm(msg,url,params,function(data){
        if(data.code==1){
            layer.msg(data.msg, {icon: 1, time: 800}, function () {
                rePage();
            });
        }else{
            layer.alert(data.msg, {icon: 2});
        }
    });
}
/**
 * 美化复选框 依赖iCheck插件
 */
function iCheck(){
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });
}
/**
 * 提交表单带回调
 */
function ajaxSubFormBack(callBack){
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    $("#form").Validform({
        btnSubmit:'#btnSub',
        ajaxPost: true,
        postonce: true,
        tipSweep: true,
        tiptype:function(msg,o,cssctl){
            var objtip=$("#validError");
            cssctl(objtip,o.type);
            objtip.text(msg);
        },
        beforeSubmit:function(curform){ //表单验证通过
            $('#validError').html();
            layer.load(2);
        },
        callback: function (data) {
            layer.closeAll();
            if (data.code == 1) {
                callBack(data);
            }else{
                $("#validError").removeClass('Validform_loading').addClass('Validform_wrong').html(data.msg);
            }
            return false;
        }
    });
}
/**
 * 提交表单
 */
function ajaxSubForm(){
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    $("#form").Validform({
        ajaxPost: true,
        postonce: true,
        tipSweep: true,
        tiptype:function(msg,o,cssctl){
            var objtip=$("#validError");
            cssctl(objtip,o.type);
            objtip.text(msg);
        },
        beforeSubmit:function(curform){ //表单验证通过
            $('#validError').html();
            layer.load(2);
        },
        callback: function (data) {
            layer.closeAll();
            if (data.code == 1) {
                Urlhref(data.url);
            }else{
                $("#validError").removeClass('Validform_loading').addClass('Validform_wrong').html(data.msg);
            }
            return false;
        }
    });
}
/**
 * 单图片上传
 * @param obj
 */
function uploadOnePic(obj,utype,callback,beforeCallback,local) {
    var uploader_obj = 'uploader_' + obj;
    uploader_obj = WebUploader.create({
        auto: true,
        resize: true,
        duplicate: true,
        swf: '/public/plugins/webuploader/Uploader.swf',
        server: "/plus/uploader/index.html?utype="+utype+"&action=uploadimage&local="+local,
        pick: "#" + obj,
        fileVal: 'upfile',
        accept: {title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', mimeTypes: 'image/gif,image/jpeg,image/jpg,image/png,image/svg'},
        compress: {width: 800, height: 1600, quality: 80, compressSize: 204800}
    });
    var len=arguments.length;
    // 当有文件被添加进队列的时候
    uploader_obj.on('beforeFileQueued', function (file) {
        if(len>3){
            return beforeCallback(file);
        }else{
            return true;
        }
    });
    uploader_obj.on('uploadSuccess', function (file, response) {
        if (response.state == 'SUCCESS') {
            callback(response);
        } else {
            alert(response.state);
        }
    });
}
/**
 * 单附件上传
 * @param obj
 */
function uploadOneFile(obj,utype,callback,beforeCallback,local) {
    var uploader_obj = 'uploader_' + obj;
    uploader_obj = WebUploader.create({
        auto: true,
        resize: true,
        duplicate: true,
        swf: '/public/plugins/webuploader/Uploader.swf',
        server: "/plus/uploader/index.html?utype="+utype+"&action=uploadfile&local="+local,
        pick: "#" + obj,
        fileVal: 'upfile'
    });
    var len=arguments.length;
    // 当有文件被添加进队列的时候
    uploader_obj.on('beforeFileQueued', function (file) {
        if(len>3){
            return beforeCallback(file);
        }else{
            return true;
        }
    });
    uploader_obj.on('uploadSuccess', function (file, response) {
        if (response.state == 'SUCCESS') {
            callback(response);
        } else {
            alert(response.state);
        }
    });
}
/**
 * 上传附件
 * @param obj 当前对象
 * @param callBack 回调函数(当前对象，返回附件地址列表)
 */
function selectAttachment(obj,callBack) {
    if(window.top !== window.self){ //嵌套
        layer = top.layer;
    }
    var title = $(obj).data('title');
    var url = $(obj).data('url');
    layer.open({
        title: title,
        type: 2,
        area: ['800px', '560px'],
        content: url,
        btn: ['确认', '取消'],
        yes: function(index,layero){
            var selectAttachment = layer.getChildFrame('#select-attachment', index);
            callBack(obj,selectAttachment.text());
            layer.close(index);
        }
    });
}
/**********************************单附件选择********************************************/
function selectSingleFile(obj,data){
    if(data==''){
        return false;
    }
    var fileinfo = data.split("###");
    var field=$(obj).data('input');
    var html = '<span>';
    html += '<a href="'+fileinfo[0]+'" target="_blank">'+fileinfo[1]+'</a>';
    html += '<a href="javascript:;" onclick="delSingleFile(this,\''+field+'\')" style="margin-left:10px;">删除</a>';
    html += '</span>';
    $("input[name='"+field+"']").val(data);
    $(obj).closest('.selectSingleFile').append(html);
    $(obj).hide();
}
//删除单附件
function delSingleFile(obj,field){
    $("input[name='"+field+"']").val('');
    $(obj).closest('.selectSingleFile').find('.uploader-file-btn').show();
    $(obj).parent().remove();
}
/**********************************单图选择********************************************/
function selectSingleJpg(obj,data){
    if(data==''){
        return false;
    }
    var field=$(obj).data('input');
    var imgHtml = '<span class="uploader-file uploader_status" style="background-image:url('+data+')">'
    imgHtml += '<a href="javascript:;" onclick="delSingleJpg(this,\''+field+'\')" class="imgBar">删除</a>';
    imgHtml += '</span>';
    $("input[name='"+field+"']").val(data);
    $(obj).closest('.selectSingleJpg').append(imgHtml);
    $(obj).hide();
}
//删除单图
function delSingleJpg(obj,field){
    $("input[name='"+field+"']").val('');
    $(obj).closest('.selectSingleJpg').find('.uploader-btn').show();
    $(obj).parent().remove();
}
/**********************************多图选择********************************************/
function selectMoreJpg(obj, data) {
    if (data == '') {
        return false;
    }
    var field=$(obj).data('input');
    $imgs = data.split('|');
    for (i = 0; i < $imgs.length; i++) {
        var html = '<div class="uploader-file uploader_status"';
        html += 'style="background-image:url(\''+$imgs[i]+'\')" data-img="'+$imgs[i]+'">';
        html += '<div class="imgBar btn-group btn-group-justified">';
        html += '<a href="javascript:;" onclick="arrowLeftMoreJpg(this,\''+field+'\')"><i class="fa fa-arrow-left"></i></a>';
        html += '<a href="javascript:;" onclick="arrowRightMoreJpg(this,\''+field+'\')"><i class="fa fa-arrow-right"></i></a>';
        html += '<a href="javascript:;" onclick="delMoreJpg(this,\''+field+'\')"><i class="fa fa-trash"></i></a>';
        html += '</div>';
        html += '</div>';
        $(obj).parent().find('.img-block').append(html);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//获取全部选择图片
function getMoreJpg(field) {
    var obj = field+'moreImg';
    console.info(obj);
    data=[];
    $('#'+obj).find('.uploader-file').each(function(){
        data.push($(this).data('img'));
    });
    return data.join('|');
}
//删除多图
function delMoreJpg(obj,field){
    $(obj).closest('.uploader-file').remove();
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//左移
function arrowLeftMoreJpg(obj,field) {
    var objParentUpload = $(obj).closest('.uploader-file');
    var prevUpload = objParentUpload.prev();
    if(prevUpload.length>0){
        prevUpload.insertAfter(objParentUpload);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
//右移
function arrowRightMoreJpg(obj,field) {
    var objParentUpload = $(obj).closest('.uploader-file');
    var nextUpload = objParentUpload.next();
    if(nextUpload.length>0){
        nextUpload.insertBefore(objParentUpload);
    }
    $("input[name='"+field+"']").val(getMoreJpg(field));
}
/**********************************多图带表单框选择********************************************/
//选择图片
function selectMoreTextImg(obj, data) {
    if (data == '') {
        return false;
    }
    var tips = $(obj).data('tips');
    var field=$(obj).data('input');
    var $imgs = data.split('|');
    for (i = 0; i < $imgs.length; i++) {
        var html = '<div class="col-sm-6"><div class="uploader-file-img">';
        html += ' <div class="image" style="background-image:url(\''+$imgs[i]+'\')" data-img="'+$imgs[i]+'">';
        html += '<div class="imgBar">';
        html += '<a href="javascript:;" onclick="arrowLeftMoreTextJpg(this,\'{$field}\')"><i class="fa fa-arrow-left"></i></a>';
        html += '<a href="javascript:;" onclick="arrowRightMoreTextJpg(this,\'{$field}\')"><i class="fa fa-arrow-right"></i></a>';
        html += '<a href="javascript:;" onclick="delMoreTextJpg(this,\'{$field}\')"><i class="fa fa-trash"></i></a>';
        html += '</div>';
        html += '</div>';
        html += '<div class="images-info"><div class="info"><div class="form-group">';
        html += '<input type="text" onblur="getMoreTextJpg(\''+field+'\');" class="form-control img-title" placeholder="'+tips+'">';
        html += '</div>';
        html += '<div class="form-group">';
        html += '<textarea onblur="getMoreTextJpg(\''+field+'\');" class="form-control img-info" rows="4" placeholder="请输入图片描述..."></textarea>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        $(obj).parent().find('.img-block').append(html);
    }
    getMoreTextJpg(field);
}
//获取全部选择图片
function getMoreTextJpg(field) {
    var obj = field+'moreTextImg';
    var img=[];
    var imgTitle=[];
    var imgInfo=[];
    $('#'+obj).find('.image').each(function(){
        img.push($(this).data('img'));
    });
    $('#'+obj).find('.img-title').each(function(){
        imgTitle.push($(this).val());
    });
    $('#'+obj).find('.img-info').each(function(){
        imgInfo.push($(this).val());
    });
    var data = img.join('|')+'###'+imgTitle.join('|')+'###'+imgInfo.join('|');
    $("input[name='"+field+"']").val(data);
}
//左移
function arrowLeftMoreTextJpg(obj,field) {
    var objParentUpload = $(obj).closest('.col-sm-6');
    var prevUpload = objParentUpload.prev();
    if(prevUpload.length>0){
        prevUpload.insertAfter(objParentUpload);
    }
    getMoreTextJpg(field);
}
//右移
function arrowRightMoreTextJpg(obj,field) {
    var objParentUpload = $(obj).closest('.col-sm-6');
    var nextUpload = objParentUpload.next();
    if(nextUpload.length>0){
        nextUpload.insertBefore(objParentUpload);
    }
    getMoreTextJpg(field);
}
//删除多图
function delMoreTextJpg(obj,field){
    $(obj).closest('.col-sm-6').remove();
    getMoreTextJpg(field);
}
/*强制保留小数点位数，第一个参数是值，第二个参数是位数*/
function changeTwoDecimal_f(x,decimal) {
    var f_x = parseFloat(x);
    if (isNaN(f_x)) {
        alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    var num='1';
    for(i=0;i<=decimal;i++){
        num+='0';
    }
    var f_x = Math.round(x * num) / num;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + decimal) {
        s_x += '0';
    }
    return s_x;
}